<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */
namespace App\Observers;

use App\Models\PointAccount;
use App\Models\User;
use App\Service\AdvanceCard\AdvanceCardService;
use App\Service\Notifications\NotificationService;

class PointAccountObserver
{
    /**
     * Handle the PointAccount "created" event.
     */
    public function created(PointAccount $pointAccount)
    {
    }

    /**
     * Handle the PointAccount "updated" event.
     */
    public function updated(PointAccount $pointAccount)
    {
        if (bcmul($pointAccount->total_point, 0.1, 2) >= $pointAccount->available_points) {
            $notify = NotificationService::make();
            $user = User::query()->find($pointAccount->user_id);
            $notify->SystemNotify($user, '您的积分已低于总积分百分之10,请注意积分获取');
            if ($pointAccount->available_points <= 0) {
                $user->member_level = 0;
                $user->save();
                $notify->SystemNotify($user, '您积分为0 拼单等级已重置');
//                AdvanceCardService::make()->refund($user->id);
            }
        }
    }

    /**
     * Handle the PointAccount "deleted" event.
     */
    public function deleted(PointAccount $pointAccount)
    {
    }

    /**
     * Handle the PointAccount "restored" event.
     */
    public function restored(PointAccount $pointAccount)
    {
    }

    /**
     * Handle the PointAccount "force deleted" event.
     */
    public function forceDeleted(PointAccount $pointAccount)
    {
    }
}
